

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>openspeech.search.beam_search_transformer_transducer &mdash; Openspeech v0.3.0 documentation</title>
  

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> Openspeech
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">GETTING STARTED</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../notes/intro.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../notes/hydra_configs.html">Openspeech’s Hydra configuration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../notes/configs.html">Openspeech’s configurations</a></li>
</ul>
<p class="caption"><span class="caption-text">OPENSPEECH MODELS</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../models/Openspeech Model.html">Openspeech Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../models/Openspeech CTC Model.html">Openspeech CTC Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../models/Openspeech Encoder Decoder Model.html">Openspeech Encoder Decoder Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../models/Openspeech Transducer Model.html">Openspeech Transducer Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../models/Openspeech Language Model.html">Openspeech Language Model</a></li>
</ul>
<p class="caption"><span class="caption-text">MODEL ARCHITECTURES</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../architectures/Conformer.html">Conformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../architectures/ContextNet.html">ContextNet</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../architectures/DeepSpeech2.html">DeepSpeech2</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../architectures/Jasper.html">Jasper</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../architectures/Listen Attend Spell.html">Listen Attend Spell Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../architectures/LSTM LM.html">LSTM Language Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../architectures/QuartzNet.html">QuartzNet Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../architectures/RNN Transducer.html">RNN Transducer Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../architectures/Transformer.html">Transformer Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../architectures/Transformer LM.html">Transformer Language Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../architectures/Transformer Transducer.html">Transformer Transducer Model</a></li>
</ul>
<p class="caption"><span class="caption-text">CORPUS</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../corpus/AISHELL-1.html">AISHELL</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../corpus/KsponSpeech.html">KsponSpeech</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../corpus/LibriSpeech.html">LibriSpeech</a></li>
</ul>
<p class="caption"><span class="caption-text">LIBRARY REFERENCE</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../modules/Callback.html">Callback</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../modules/Criterion.html">Criterion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../modules/Data Augment.html">Data Augment</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../modules/Feature Transform.html">Feature Transform</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../modules/Datasets.html">Datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../modules/Data Loaders.html">Data Loaders</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../modules/Decoders.html">Decoders</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../modules/Encoders.html">Encoders</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../modules/Modules.html">Modules</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../modules/Optim.html">Optim</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../modules/Search.html">Search</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../modules/Tokenizers.html">Tokenizers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../modules/Metric.html">Metric</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">Openspeech</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>openspeech.search.beam_search_transformer_transducer</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for openspeech.search.beam_search_transformer_transducer</h1><div class="highlight"><pre>
<span></span><span class="c1"># MIT License</span>
<span class="c1">#</span>
<span class="c1"># Copyright (c) 2021 Soohwan Kim and Sangchun Ha and Soyoung Cho</span>
<span class="c1">#</span>
<span class="c1"># Permission is hereby granted, free of charge, to any person obtaining a copy</span>
<span class="c1"># of this software and associated documentation files (the &quot;Software&quot;), to deal</span>
<span class="c1"># in the Software without restriction, including without limitation the rights</span>
<span class="c1"># to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
<span class="c1"># copies of the Software, and to permit persons to whom the Software is</span>
<span class="c1"># furnished to do so, subject to the following conditions:</span>
<span class="c1">#</span>
<span class="c1"># The above copyright notice and this permission notice shall be included in all</span>
<span class="c1"># copies or substantial portions of the Software.</span>
<span class="c1">#</span>
<span class="c1"># THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
<span class="c1"># IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<span class="c1"># FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
<span class="c1"># AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<span class="c1"># LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span>
<span class="c1"># OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE</span>
<span class="c1"># SOFTWARE.</span>

<span class="kn">import</span> <span class="nn">torch</span>

<span class="kn">from</span> <span class="nn">openspeech.search.beam_search_base</span> <span class="kn">import</span> <span class="n">OpenspeechBeamSearchBase</span>
<span class="kn">from</span> <span class="nn">openspeech.decoders</span> <span class="kn">import</span> <span class="n">TransformerTransducerDecoder</span>


<div class="viewcode-block" id="BeamSearchTransformerTransducer"><a class="viewcode-back" href="../../../modules/Search.html#openspeech.search.beam_search_transformer_transducer.BeamSearchTransformerTransducer">[docs]</a><span class="k">class</span> <span class="nc">BeamSearchTransformerTransducer</span><span class="p">(</span><span class="n">OpenspeechBeamSearchBase</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Transformer Transducer Beam Search</span>
<span class="sd">    Reference: RNN-T FOR LATENCY CONTROLLED ASR WITH IMPROVED BEAM SEARCH (https://arxiv.org/pdf/1911.01629.pdf)</span>

<span class="sd">    Args: joint, decoder, beam_size, expand_beam, state_beam, blank_id</span>
<span class="sd">        joint: joint `encoder_outputs` and `decoder_outputs`</span>
<span class="sd">        decoder (TransformerTransducerDecoder): base decoder of transformer transducer model.</span>
<span class="sd">        beam_size (int): size of beam.</span>
<span class="sd">        expand_beam (int): The threshold coefficient to limit the number</span>
<span class="sd">        of expanded hypotheses that are added in A (process_hyp).</span>
<span class="sd">        state_beam (int): The threshold coefficient in log space to decide if hyps in A (process_hyps)</span>
<span class="sd">        is likely to compete with hyps in B (ongoing_beams)</span>
<span class="sd">        blank_id (int): blank id</span>

<span class="sd">    Inputs: encoder_outputs, max_length</span>
<span class="sd">        encoder_outputs (torch.FloatTensor): A output sequence of encoders. `FloatTensor` of size</span>
<span class="sd">            ``(batch, seq_length, dimension)``</span>
<span class="sd">        max_length (int): max decoding time step</span>

<span class="sd">    Returns:</span>
<span class="sd">        * predictions (torch.LongTensor): model predictions.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span>
            <span class="n">joint</span><span class="p">,</span>
            <span class="n">decoder</span><span class="p">:</span> <span class="n">TransformerTransducerDecoder</span><span class="p">,</span>
            <span class="n">beam_size</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">3</span><span class="p">,</span>
            <span class="n">expand_beam</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">2.3</span><span class="p">,</span>
            <span class="n">state_beam</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">4.6</span><span class="p">,</span>
            <span class="n">blank_id</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">3</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">BeamSearchTransformerTransducer</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">decoder</span><span class="p">,</span> <span class="n">beam_size</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">joint</span> <span class="o">=</span> <span class="n">joint</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">forward_step</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">decoder</span><span class="o">.</span><span class="n">forward_step</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">expand_beam</span> <span class="o">=</span> <span class="n">expand_beam</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">state_beam</span> <span class="o">=</span> <span class="n">state_beam</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">blank_id</span> <span class="o">=</span> <span class="n">blank_id</span>

<div class="viewcode-block" id="BeamSearchTransformerTransducer.forward"><a class="viewcode-back" href="../../../modules/Search.html#openspeech.search.beam_search_transformer_transducer.BeamSearchTransformerTransducer.forward">[docs]</a>    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">encoder_outputs</span><span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">Tensor</span><span class="p">,</span> <span class="n">max_length</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Beam search decoding.</span>

<span class="sd">        Inputs: encoder_outputs, max_length</span>
<span class="sd">            encoder_outputs (torch.FloatTensor): A output sequence of encoders. `FloatTensor` of size</span>
<span class="sd">            ``(batch, seq_length, dimension)``</span>
<span class="sd">            max_length (int): max decoding time step</span>

<span class="sd">        Returns:</span>
<span class="sd">            * predictions (torch.LongTensor): model predictions.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">hypothesis</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
        <span class="n">hypothesis_score</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>

        <span class="k">for</span> <span class="n">batch_idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">encoder_outputs</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="mi">0</span><span class="p">)):</span>
            <span class="n">blank</span> <span class="o">=</span> <span class="p">(</span>
                    <span class="n">torch</span><span class="o">.</span><span class="n">ones</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">device</span><span class="o">=</span><span class="n">encoder_outputs</span><span class="o">.</span><span class="n">device</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">long</span><span class="p">)</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">blank_id</span>
            <span class="p">)</span>
            <span class="n">step_input</span> <span class="o">=</span> <span class="p">(</span>
                    <span class="n">torch</span><span class="o">.</span><span class="n">ones</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">device</span><span class="o">=</span><span class="n">encoder_outputs</span><span class="o">.</span><span class="n">device</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">long</span><span class="p">)</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">sos_id</span>
            <span class="p">)</span>
            <span class="n">hyp</span> <span class="o">=</span> <span class="p">{</span>
                <span class="s2">&quot;prediction&quot;</span><span class="p">:</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">sos_id</span><span class="p">],</span>
                <span class="s2">&quot;logp_score&quot;</span><span class="p">:</span> <span class="mf">0.0</span><span class="p">,</span>
            <span class="p">}</span>
            <span class="n">ongoing_beams</span> <span class="o">=</span> <span class="p">[</span><span class="n">hyp</span><span class="p">]</span>

            <span class="k">for</span> <span class="n">t_step</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">max_length</span><span class="p">):</span>
                <span class="n">process_hyps</span> <span class="o">=</span> <span class="n">ongoing_beams</span>
                <span class="n">ongoing_beams</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>

                <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
                    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">ongoing_beams</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">beam_size</span><span class="p">:</span>
                        <span class="k">break</span>

                    <span class="n">a_best_hyp</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">process_hyps</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="s2">&quot;logp_score&quot;</span><span class="p">]</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="s2">&quot;prediction&quot;</span><span class="p">]))</span>

                    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">ongoing_beams</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                        <span class="n">b_best_hyp</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span>
                            <span class="n">ongoing_beams</span><span class="p">,</span>
                            <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="s2">&quot;logp_score&quot;</span><span class="p">]</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="s2">&quot;prediction&quot;</span><span class="p">]),</span>
                        <span class="p">)</span>

                        <span class="n">a_best_prob</span> <span class="o">=</span> <span class="n">a_best_hyp</span><span class="p">[</span><span class="s2">&quot;logp_score&quot;</span><span class="p">]</span>
                        <span class="n">b_best_prob</span> <span class="o">=</span> <span class="n">b_best_hyp</span><span class="p">[</span><span class="s2">&quot;logp_score&quot;</span><span class="p">]</span>

                        <span class="k">if</span> <span class="n">b_best_prob</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_beam</span> <span class="o">+</span> <span class="n">a_best_prob</span><span class="p">:</span>
                            <span class="k">break</span>

                    <span class="n">process_hyps</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">a_best_hyp</span><span class="p">)</span>

                    <span class="n">step_input</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">a_best_hyp</span><span class="p">[</span><span class="s2">&quot;prediction&quot;</span><span class="p">][</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
                    <span class="n">step_lengths</span> <span class="o">=</span> <span class="n">encoder_outputs</span><span class="o">.</span><span class="n">new_tensor</span><span class="p">([</span><span class="mi">0</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">long</span><span class="p">)</span>

                    <span class="n">step_outputs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forward_step</span><span class="p">(</span><span class="n">step_input</span><span class="p">,</span> <span class="n">step_lengths</span><span class="p">)</span><span class="o">.</span><span class="n">squeeze</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">squeeze</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
                    <span class="n">log_probs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">joint</span><span class="p">(</span><span class="n">encoder_outputs</span><span class="p">[</span><span class="n">batch_idx</span><span class="p">,</span> <span class="n">t_step</span><span class="p">,</span> <span class="p">:],</span> <span class="n">step_outputs</span><span class="p">)</span>

                    <span class="n">topk_targets</span><span class="p">,</span> <span class="n">topk_idx</span> <span class="o">=</span> <span class="n">log_probs</span><span class="o">.</span><span class="n">topk</span><span class="p">(</span><span class="n">k</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">beam_size</span><span class="p">)</span>

                    <span class="k">if</span> <span class="n">topk_idx</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">!=</span> <span class="n">blank</span><span class="p">:</span>
                        <span class="n">best_logp</span> <span class="o">=</span> <span class="n">topk_targets</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">best_logp</span> <span class="o">=</span> <span class="n">topk_targets</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>

                    <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">topk_targets</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="mi">0</span><span class="p">)):</span>
                        <span class="n">topk_hyp</span> <span class="o">=</span> <span class="p">{</span>
                            <span class="s2">&quot;prediction&quot;</span><span class="p">:</span> <span class="n">a_best_hyp</span><span class="p">[</span><span class="s2">&quot;prediction&quot;</span><span class="p">][:],</span>
                            <span class="s2">&quot;logp_score&quot;</span><span class="p">:</span> <span class="n">a_best_hyp</span><span class="p">[</span><span class="s2">&quot;logp_score&quot;</span><span class="p">]</span> <span class="o">+</span> <span class="n">topk_targets</span><span class="p">[</span><span class="n">j</span><span class="p">],</span>
                        <span class="p">}</span>

                        <span class="k">if</span> <span class="n">topk_idx</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">blank_id</span><span class="p">:</span>
                            <span class="n">ongoing_beams</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">topk_hyp</span><span class="p">)</span>
                            <span class="k">continue</span>

                        <span class="k">if</span> <span class="n">topk_targets</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="n">best_logp</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">expand_beam</span><span class="p">:</span>
                            <span class="n">topk_hyp</span><span class="p">[</span><span class="s2">&quot;prediction&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">topk_idx</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">item</span><span class="p">())</span>
                            <span class="n">process_hyps</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">topk_hyp</span><span class="p">)</span>

            <span class="n">ongoing_beams</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span>
                <span class="n">ongoing_beams</span><span class="p">,</span>
                <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="s2">&quot;logp_score&quot;</span><span class="p">]</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="s2">&quot;prediction&quot;</span><span class="p">]),</span>
                <span class="n">reverse</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
            <span class="p">)[</span><span class="mi">0</span><span class="p">]</span>

            <span class="n">hypothesis</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">LongTensor</span><span class="p">(</span><span class="n">ongoing_beams</span><span class="p">[</span><span class="s2">&quot;prediction&quot;</span><span class="p">][</span><span class="mi">1</span><span class="p">:]))</span>
            <span class="n">hypothesis_score</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ongoing_beams</span><span class="p">[</span><span class="s2">&quot;logp_score&quot;</span><span class="p">]</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">ongoing_beams</span><span class="p">[</span><span class="s2">&quot;prediction&quot;</span><span class="p">]))</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fill_sequence</span><span class="p">(</span><span class="n">hypothesis</span><span class="p">)</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, Kim, Soohwan and Ha, Sangchun and Cho, Soyoung.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>